<template>
	<view>
		<text class="secret-info" v-if="transform != 'translateY(0)'">蓝海news由lzzy.soft22制作</text>
		<view class="welcome-container" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"
			:style="bodyStyle">
			<image src="../../static/icons(1)/icon.png" class="icon-welcome"></image>
			<text class="welcome">WELCOME</text>
			<text class="sub-welcome">欢迎加入All blue</text>
			<view class="upload-pic-container" @click="clickChooseImage">
				<image :src="userAvatar" mode="aspectFill" class="avatar"></image>
				<text class="input-holder">{{avatarUrl}}</text>
			</view>
			<input type="text" class="input-register-info" placeholder-class="input-holder" placeholder="请输入昵称"
				id="nick" @input="inputGet">
			<input type="text" class="input-register-info" placeholder-class="input-holder" placeholder="请输入邮箱"
				id="email" @input="inputGet">
			<input type="text" class="input-register-info" placeholder-class="input-holder" placeholder="请输入用户名"
				id="user" @input="inputGet">
			<input type="password" class="input-register-info" placeholder-class="input-holder" placeholder="请输入密码"
				id="password" @input="inputGet">
			<text class="btn" @click="clickRegister">注册</text>
		</view>
	</view>
</template>

<script>
	let pointY = 0
	let pic = ""
	export default {
		data() {
			return {
				avatarUrl: "请点击选择图片上传",
				userAvatar: "../../static/icons(1)/default_user.png",
				transform: "translateY(0)",
				transition: '',
				nick: "",
				email: "",
				user: "",
				password: ""
			}
		},
		computed: {
			bodyStyle: function() {
				return {
					"transform": this.transform,
					"transition": this.transition
				}
			}
		},
		methods: {
			checkValid() {
				if (this.nick.length == 0) {
					this.nick = this.user
				}
				if (!this.$util.checkLength(this.user, 6, "用户名") || !this.$util.checkLength(this.password, 6, "密码")) {
					return false
				}
				let regex = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/
				if (!regex.test(this.email)) {
					uni.showToast({
						title: '电子邮箱格式不正确',
						icon: 'none'
					});
					return false
				}
				return true
			},
			clickRegister() {
				if (!this.checkValid()) {
					return
				}
				let data = {
					"nick_name": this.nick,
					"user_name": this.user,
					"password": this.password,
					"avatar": pic,
					"email": this.email
				}
				// console.log(data);
				let url = this.$params.host + this.$params.action_register
				this.$request.post(url, data, res =>{
					uni.showToast({
						title: res.message,
						icon:'none'
					});
					if(!res.success){
						return
					}
					let app = getApp()
					app.globalData.uid = res.data.id
					app.globalData.token = res.data.token
					app.globalData.avatar = res.data.avatar
					app.globalData.type = res.data.type
					app.globalData.userName = res.data.userName
					app.globalData.nickName = res.data.nickName
					app.globalData.email = res.data.email
					this.$noti.post(this.$params.$noti_login_status)
					uni.navigateBack();
				},() => {})
			},
			inputGet(event) {
				let id = event.currentTarget.id //"nick"
				//如果要把字符串作为变量名称，那么引用该变量就要使用模板字符串，将该变量名字字符串作为插值，并在外层使用中括号
				this[`${id}`] = event.detail.value
			},
			clickChooseImage() {
				let url = this.$params.host + this.$params.action_upload
				uni.chooseImage({
					success: res => {
						let filePath = res.tempFilePaths[0]
						uni.showLoading({
							title: '上传头像......',
							mask: false
						});
						this.$request.postFile(url, 'file', filePath, {}, res => {
							uni.hideLoading()
							let json = JSON.parse(res.data)
							if (json.success) {
								this.avatarUrl = json.data
								pic = json.data
								this.userAvatar = this.$params.host + json.data
							}
						})
					}
				})
			},
			touchStart(e) {
				pointY = e.touches[0].clientY
			},
			touchMove(e) {
				let distance = e.touches[0].clientY - pointY
				if (distance > 150) {
					distance = 150
				}
				if (distance < 50) {
					return
				}
				this.transform = `translateY(${distance}rpx)`
				this.transition = ""
			},
			touchEnd(e) {
				this.transform = 'translateY(0)'
				this.transition = 'transform 800ms linear'
			}

		}
	}
</script>

<style>
	@import url("register.css");
</style>
